package no.sogl.importer;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import no.sogl.domain.Person;
import no.sogl.reference.Gender;
import no.sogl.util.ExcelHelper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

public class KarusellExporter {
    private static final Log log = LogFactory.getLog(KarusellExporter.class);
    private static AbstractApplicationContext ctx;
    private static Map<String, Person> dup = new HashMap<String, Person>();

    public static void main(String[] args) throws Exception {
        log.info("Starting karusell exporter");
        ctx = new ClassPathXmlApplicationContext(new String[] { "classpath:META-INF/spring/applicationContext.xml" });
        ctx.registerShutdownHook();
        KarusellExporter exporter = ctx.getBean(KarusellExporter.class);
        // exporter.exportN3MemberSince();
        // exporter.exportN3People();
        exporter.exportPeople();
        // exporter.exportPeopleSelection();
    }

    @Transactional
    public void exportPeople() throws Exception {
        final String tagName = "Karusell 2012";
        HSSFWorkbook wb = new HSSFWorkbook();
        Map<String, CellStyle> styles = ExcelHelper.createStyles(wb);
        int rowNo = 1;
        HSSFSheet sheet = wb.createSheet("Ark 1");
        createHeaders(styles, sheet);

        for (Person person : Person.findAllPeople()) {
            if (!person.hasTag(tagName)) continue;
            if (person.getBirthYear() == null ) {
                System.out.println("****************Missing birthyear: " + person);
                continue;
            }
            if (dup.get(person.getLongInitials()) != null)
                System.out.println("****************Duplicate found (" + person.getLongInitials() + "): " + person);
            dup.put(person.getLongInitials(), person);
            System.out.println(person.getLongInitials());
            addRow(person, sheet, rowNo++, styles);
        }

        wb.write(new FileOutputStream(new File("/Users/espenwest/Documents/sogl/karusell"
                + DateTimeFormat.forPattern("yyyyMMdd").print(new DateTime()) + ".xls")));
    }

    private void addRow(Person person, HSSFSheet sheet, int rowNo, Map<String, CellStyle> styles) {
        Row row = sheet.createRow(rowNo);
        DateTime now = new DateTime();
        ExcelHelper.createCell(row, 0, person.getFirstName(), styles.get("left"));
        ExcelHelper.createCell(row, 1, person.getLastName(), styles.get("left"));
        if (person.getBirthYear() > 2005) ExcelHelper.createCell(row, 2, "B" + (now.getYear() - person.getBirthYear()),
            styles.get("left"));
        else ExcelHelper.createCell(row, 2, (person.getGender() == Gender.Female ? "J" : "G")
                + (now.getYear() - person.getBirthYear()), styles.get("left"));
        ExcelHelper.createCell(row, 5, person.getGender() == Gender.Female ? "K" : "M", styles.get("left"));
        if (person.getTags().size() > 0)
            ExcelHelper.createCell(row, 10, StringUtils.collectionToCommaDelimitedString(person.getTags()), styles.get("left"));
        ExcelHelper.createCell(row, 11, person.getMembership() != null ? "J" : "N", styles.get("left"));
        ExcelHelper.createCell(row, 12, person.getMobile(), styles.get("left"));
        ExcelHelper.createCell(row, 13, person.getEmail(), styles.get("left"));
        ExcelHelper.createCell(row, 14, person.getBirthYear(), styles.get("left"));

    }

    private void createHeaders(Map<String, CellStyle> styles, HSSFSheet sheet) {
        Row row = sheet.createRow(0);
        row.setHeightInPoints(16);
        ExcelHelper.createCell(row, 0, "Fornavn", styles.get("header"));
        sheet.setColumnWidth(0, 15 * 256);
        ExcelHelper.createCell(row, 1, "Etternavn", styles.get("header"));
        sheet.setColumnWidth(1, 15 * 256);
        ExcelHelper.createCell(row, 2, "Klasse", styles.get("header"));
        sheet.setColumnWidth(2, 15 * 256);
        ExcelHelper.createCell(row, 3, "Klubb navn", styles.get("header"));
        sheet.setColumnWidth(3, 15 * 256);
        ExcelHelper.createCell(row, 4, "Løype/distanse", styles.get("header"));
        sheet.setColumnWidth(4, 15 * 256);
        ExcelHelper.createCell(row, 5, "Kjønn", styles.get("header"));
        sheet.setColumnWidth(5, 8 * 256);
        ExcelHelper.createCell(row, 6, "Startno", styles.get("header"));
        sheet.setColumnWidth(6, 11 * 256);
        ExcelHelper.createCell(row, 7, "Brikkenr", styles.get("header"));
        sheet.setColumnWidth(7, 11 * 256);
        ExcelHelper.createCell(row, 8, "Starttid (HH:MM:SS)", styles.get("header"));
        sheet.setColumnWidth(8, 11 * 256);
        ExcelHelper.createCell(row, 9, "Kid", styles.get("header"));
        sheet.setColumnWidth(9, 11 * 256);
        ExcelHelper.createCell(row, 10, "Tags", styles.get("header"));
        sheet.setColumnWidth(10, 20 * 256);
        ExcelHelper.createCell(row, 11, "Medlem?", styles.get("header"));
        sheet.setColumnWidth(11, 12 * 256);
        ExcelHelper.createCell(row, 12, "Mobil", styles.get("header"));
        sheet.setColumnWidth(12, 30 * 256);
        ExcelHelper.createCell(row, 13, "Mail", styles.get("header"));
        sheet.setColumnWidth(13, 40 * 256);
        ExcelHelper.createCell(row, 14, "Fødselsår", styles.get("header"));
        sheet.setColumnWidth(14, 12 * 256);
    }
}
